package com.nativetalk.service.four.search;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.SearchCourseAndWeikeResponseDto;
import com.nativetalk.bean.course.TsNtCourseInfoList;
import com.nativetalk.bean.dict.TsNtDictDeatil;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.search.TdNtSearchHistory;
import com.nativetalk.bean.search.TdNtSearchHistoryList;
import com.nativetalk.bean.teacher.TdNtTeacherList;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtSearchHisoryDao;
import com.nativetalk.dao.TdNtTeacherDao;
import com.nativetalk.dao.TsNtCourseInfoDao;
import com.nativetalk.dto.weike.Weike;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by lizhun on 2016/10/28.
 */
@Service
public class FourSearchServiceImpl implements FourSearchService {
    private static Logger logger = Logger.getLogger(FourSearchServiceImpl.class);
    @Resource
    private TdNtSearchHisoryDao tdNtSearchHisoryDao;
    @Resource
    private TsNtCourseInfoDao tsNtCourseInfoDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;

    //Map<String, TsNtSetmealIndex> tsNtSetmealSynopisMap = (Map<String, TsNtSetmealIndex>) MemcachedUtils.get(MemcachedKey.tsNtSetmealIndexMap);
    @Override
    public RetInfo findHotKeyword() {
        String logInfo = this.getClass().getName() + ":addLiveHis:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            List<TdNtSearchHistoryList> tdNtSearchHistoryLists = tdNtSearchHisoryDao.findHotKeyword();
            if (tdNtSearchHistoryLists != null && tdNtSearchHistoryLists.size() > 0) {
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("成功");
                retInfo.setObj(tdNtSearchHistoryLists);
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("暂无热门搜索");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo search(Page page, TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":search:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtDictDeatil> tsNtDictDeatilsMap = (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);
            Map<String, Object> result = new HashMap<>();
            page.setDict_id(0);

            // 添加搜索历史记录
            if (tdNtMember != null) {
                TdNtSearchHistory tdNtSearchHistory = new TdNtSearchHistory();
                tdNtSearchHistory.setMemb_id(tdNtMember.getMemb_id());
                tdNtSearchHistory.setSea_content(page.getObj().toString());
                tdNtSearchHistory.setSea_his_month(CommonUtil.getMonth(new Date()));
                tdNtSearchHisoryDao.insertTdNtSearchHistory(tdNtSearchHistory);
            }

            // 根据关键词 搜索老师
            page.setCurrentPage(0);
            int totalRecord = tdNtTeacherDao.searchTdNtTeacherListSize(page);//总条数
            int totalPage = page.getTotalPage(totalRecord);//总页数
            List<TdNtTeacherList> tdNtTeacherLists = tdNtTeacherDao.searchTdNtTeacherList(page);
            for (int i = 0; i < tdNtTeacherLists.size(); i++) {
                if (tdNtTeacherLists.get(i).getDict_id() != null) {
                    TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(tdNtTeacherLists.get(i).getDict_id());
                    tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                } else {
                    TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                    tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                }
            }
            result.put("teachers", tdNtTeacherLists);
            result.put("tea_totalPage", totalPage);

            /**
             * 根据关键词 搜索课程
             */
            int cou_totalRecord = tsNtCourseInfoDao.findCourseSize(page);//总条数
            int cou_totalPage = page.getTotalPage(cou_totalRecord);//总页数
            List<TsNtCourseInfoList> tsNtCourseInfoLists = tsNtCourseInfoDao.findCoursePage(page);
            result.put("cou_totalPage", cou_totalPage);
            result.put("courses", tsNtCourseInfoLists);

            //根据关键词 搜索微课 (搜索title 和 title_student)
            Integer count = tsNtCourseInfoDao.searchWeikeCount(page);
            List<Weike> weikes = tsNtCourseInfoDao.searchWeike(page);
            result.put("weike_totalPage", page.getTotalPage(count));
            result.put("weikes", weikes);

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取列表成功");
            retInfo.setObj(result);



        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo coursesearch(Page page) {
        String logInfo = this.getClass().getName() + ":coursesearch:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            /**
             * 根据关键词搜索课程
             */

            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tsNtCourseInfoDao.findCourseSize(page);//总条数

            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord / pageSize;
            } else {
                totalPage = (totalRecord / pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (currentPage <= 1) {
                page.setCurrentPage(1);
            }
            page.setCurrentPage((page.getCurrentPage() - 1) * pageSize);
            List<TsNtCourseInfoList> tsNtCourseInfoLists = tsNtCourseInfoDao.findCoursePage(page);
            result.put("cou_totalPage", totalPage);
            result.put("courses", tsNtCourseInfoLists);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取列表成功");
            retInfo.setObj(result);


        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    /**
     * 根据关键词搜索微课
     */
    public RetInfo searchWeike(Page page) {
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();

            //根据关键词 搜索微课 (搜索title 和 title_student)
            Integer count = tsNtCourseInfoDao.searchWeikeCount(page);
            List<Weike> weikes = tsNtCourseInfoDao.searchWeike(page);
            result.put("weike_totalPage", page.getTotalPage(count));
            result.put("weikes", weikes);

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取列表成功");
            retInfo.setObj(result);

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        return retInfo;
    }

    // v4.3 首页搜索
    public RetInfo search13(Page page, TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":search:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtDictDeatil> tsNtDictDeatilsMap = (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);
            Map<String, Object> result = new HashMap<>();
            page.setDict_id(0);

            // 添加搜索历史记录
            if (tdNtMember != null) {
                TdNtSearchHistory tdNtSearchHistory = new TdNtSearchHistory();
                tdNtSearchHistory.setMemb_id(tdNtMember.getMemb_id());
                tdNtSearchHistory.setSea_content(page.getObj().toString());
                tdNtSearchHistory.setSea_his_month(CommonUtil.getMonth(new Date()));
                tdNtSearchHisoryDao.insertTdNtSearchHistory(tdNtSearchHistory);
            }

            // 根据关键词 搜索老师
            page.setCurrentPage(0);
            int totalRecord = tdNtTeacherDao.searchTdNtTeacherListSize(page);//总条数
            List<TdNtTeacherList> tdNtTeacherLists = tdNtTeacherDao.searchTdNtTeacherList(page);
            for (int i = 0; i < tdNtTeacherLists.size(); i++) {
                if (tdNtTeacherLists.get(i).getDict_id() != null) {
                    TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(tdNtTeacherLists.get(i).getDict_id());
                    tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                } else {
                    TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                    tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                }
            }
            result.put("teachers", tdNtTeacherLists);
            result.put("tea_totalPage", page.getTotalPage(totalRecord));

            //根据关键词 搜索课程和微课 (搜索title 和 title_student)
            List<TsNtCourseInfoList> tsNtCourseInfoLists = tsNtCourseInfoDao.findCoursePage(page);
            Integer cou_totalPage = tsNtCourseInfoDao.searchCourseAndWeikeCount(page);
            List<SearchCourseAndWeikeResponseDto> courses = tsNtCourseInfoDao.searchCourseAndWeike(page);
            if (courses == null) {
                courses = new ArrayList<>();
            }
            result.put("cou_totalPage", page.getTotalPage(cou_totalPage));
            result.put("courses", courses);

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取列表成功");
            retInfo.setObj(result);


        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    // v4.3 首页-搜索-课程和微课
    @Override
    public RetInfo courseSearch13(Page page) {
        String logInfo = this.getClass().getName() + ":coursesearch:";
        // 类路径+方法名
        logger.info("====== begin ======");
        RetInfo retInfo = new RetInfo();
        try {
            //根据关键词搜索课程和微课
            Map<String, Object> result = new HashMap<>();
            Integer cou_totalPage = tsNtCourseInfoDao.searchCourseAndWeikeCount(page);
            List<SearchCourseAndWeikeResponseDto> list = tsNtCourseInfoDao.searchCourseAndWeike(page);
            if (list == null) {
                list = new ArrayList<>();
            }
            result.put("cou_totalPage", page.getTotalPage(cou_totalPage));
            result.put("courses", list);

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取列表成功");
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("====== end ======");
        return retInfo;
    }
}
